package com.haiyuancloud.auth.service.impl;

import com.baidu.fsg.uid.impl.CachedUidGenerator;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.haiyuancloud.auth.mapper.TbLogLoginMapper;
import com.haiyuancloud.auth.service.TbLogLoginService;
import com.haiyuancloud.common.core.entity.HaiyuanAuthUser;
import com.haiyuancloud.common.core.entity.system.TbLogLogin;
import com.haiyuancloud.common.core.utils.HaiyuanUtil;
import com.haiyuancloud.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

@Slf4j
@Service("com.haiyuancloud.auth.service.TbLogLoginService")
public class TbLogLoginServiceImpl implements TbLogLoginService {

    public final static String DEF_LOG_DS_ID = "LOG_447943785935011005";;
    public final static String DEF_MASTER_DS_ID = "master";;

    @Resource
    private CachedUidGenerator uidGenerator;

    @Resource
    private TbLogLoginMapper tbLogLoginMapper;

    @Override
    public  void insert (HaiyuanAuthUser currentUser, Integer src, Integer flag, String message){
        HttpServletRequest request = HaiyuanUtil.getHttpServletRequest();
        String clientId = request.getHeader("authorization");
        String clientAgent = request.getHeader("user-agent");
        try {
            TbLogLogin tbLogLogin = new TbLogLogin();
            tbLogLogin.setSId(currentUser.getLoginId());
            tbLogLogin.setILogStaffId(currentUser.getUserId());
            tbLogLogin.setSLogStaffName(currentUser.getUsername());
            tbLogLogin.setSLogStaffAcc(currentUser.getUserAcc());
            tbLogLogin.setILogDomainId(currentUser.getDomainId());
            tbLogLogin.setSLogDomainName(currentUser.getDomainName());
            tbLogLogin.setILogDeptId(currentUser.getDeptId());
            tbLogLogin.setSLogDeptName(currentUser.getDeptPathName());
            tbLogLogin.setDLogTime(new Date());
            tbLogLogin.setILogSource(src);
            tbLogLogin.setILogFlag(flag);
            tbLogLogin.setSLogMsg(message);
            tbLogLogin.setI_CREATE_YEAR(ObjectUtil.toInteger(ObjectUtil.getNowTimeInStyle("yyyy")));
            tbLogLogin.setI_CREATE_MONTH(ObjectUtil.toInteger(ObjectUtil.getNowTimeInStyle("MM")));
            tbLogLogin.setI_CREATE_DAY(ObjectUtil.toInteger(ObjectUtil.getNowTimeInStyle("dd")));
            tbLogLogin.setI_CREATE_HOUR(ObjectUtil.toInteger(ObjectUtil.getNowTimeInStyle("HH")));
            tbLogLogin.setI_CREATE_WEEK(ObjectUtil.getNowDayOfWeek());
            tbLogLogin.setTENANT_ID(currentUser.getTenantId());
            tbLogLogin.setCREATE_PARTITION(currentUser.getCreateParttion());

            try {
                tbLogLogin.setSServerIp("" + InetAddress.getLocalHost());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            tbLogLogin.setSClientId(clientId);
            tbLogLogin.setSClientIp(HaiyuanUtil.getHttpServletRequestIpAddress());
            tbLogLogin.setSClientVersion(clientAgent);
            DynamicDataSourceContextHolder.push(DEF_LOG_DS_ID);
            tbLogLoginMapper.insert(tbLogLogin);
            DynamicDataSourceContextHolder.push(DEF_MASTER_DS_ID);
        }catch ( Exception e){
            log.error("成功保存登录异常日志异常."+e.getMessage());
        }
    }

    @Override
    public void insert(String dsId, String userAcc, Integer src, Integer flag, String message) {
        TbLogLogin tbLogLogin =  getErrorTbLogLogin( userAcc, src,  flag,  message);
        if(ObjectUtil.isNotNull(tbLogLogin)){
            DynamicDataSourceContextHolder.push(DEF_LOG_DS_ID);
            tbLogLoginMapper.insert(tbLogLogin);
            DynamicDataSourceContextHolder.push(DEF_MASTER_DS_ID);
        }
    }

    @Override
    public void insert( String userAcc,Integer src, Integer flag, String message) {

        TbLogLogin tbLogLogin =  getErrorTbLogLogin( userAcc, src,  flag,  message);
        if(ObjectUtil.isNotNull(tbLogLogin)){
            DynamicDataSourceContextHolder.push(DEF_LOG_DS_ID);
            tbLogLoginMapper.insert(tbLogLogin);
            DynamicDataSourceContextHolder.push(DEF_MASTER_DS_ID);
        }
    }

    private TbLogLogin getErrorTbLogLogin(String userAcc,Integer src, Integer flag, String message){
        HttpServletRequest request = HaiyuanUtil.getHttpServletRequest();
        String clientId = request.getHeader("authorization");
        String clientAgent = request.getHeader("user-agent");
        try {
            TbLogLogin tbLogLogin = new TbLogLogin();
            tbLogLogin.setSId(String.valueOf(uidGenerator.getUID()));
            tbLogLogin.setSLogStaffAcc(userAcc);
            tbLogLogin.setDLogTime(new Date());
            tbLogLogin.setILogSource(src);
            tbLogLogin.setILogFlag(flag);
            tbLogLogin.setSLogMsg(message);
            tbLogLogin.setI_CREATE_YEAR(ObjectUtil.toInteger(ObjectUtil.getNowTimeInStyle("yyyy")));
            tbLogLogin.setI_CREATE_MONTH(ObjectUtil.toInteger(ObjectUtil.getNowTimeInStyle("MM")));
            tbLogLogin.setI_CREATE_DAY(ObjectUtil.toInteger(ObjectUtil.getNowTimeInStyle("dd")));
            tbLogLogin.setI_CREATE_HOUR(ObjectUtil.toInteger(ObjectUtil.getNowTimeInStyle("HH")));
            tbLogLogin.setI_CREATE_WEEK(ObjectUtil.getNowDayOfWeek());
            try {
                tbLogLogin.setSServerIp("" + InetAddress.getLocalHost());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            tbLogLogin.setSClientId(clientId);
            tbLogLogin.setSClientIp(HaiyuanUtil.getHttpServletRequestIpAddress());
            tbLogLogin.setSClientVersion(clientAgent);
            return tbLogLogin;
        }catch ( Exception e){
            log.error("保存登录异常日志异常."+e.getMessage());
        }
        return null;
    }
}
